package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.QueryMemberInfoService;
import cc.rengu.igas.momp.facade.request.QueryMemberCouponInfoRequest;
import cc.rengu.igas.momp.facade.request.QueryMemberDetailInfoRequest;
import cc.rengu.igas.momp.facade.request.QueryMemberInfoRequest;
import cc.rengu.igas.momp.facade.response.QueryMemberCouponInfoResponse;
import cc.rengu.igas.momp.facade.response.QueryMemberDetailInfoResponse;
import cc.rengu.igas.momp.facade.response.QueryMemberInfoResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 会员相关接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/24 11:53
 */
@SuppressWarnings("unchecked")
public class QueryMemberInfoServiceImpl implements QueryMemberInfoService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * 会员列表信息查询接口Dubbo接口实现
     *
     * @param queryMemberInfoRequest 会员信息查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryMemberInfoResponse> 会员信息查询接口应答对象
     * @author Jinan Liu
     * @date 2020/6/19 15:17
     */
    @Override
    public Result<QueryMemberInfoResponse> queryMemberInfo(QueryMemberInfoRequest queryMemberInfoRequest) {

        DubboService<QueryMemberInfoRequest> dubboService = new DubboServiceImpl();
        Result<QueryMemberInfoResponse> queryMemberInfoResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_MEMBER_INFO.getTxnNum().equals(queryMemberInfoRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryMemberInfoResponse> dubboResult = dubboService.callAtomService(queryMemberInfoRequest,
                    MompTxnNumEnum.QUERY_MEMBER_INFO.getService(), QueryMemberInfoResponse.class);
            queryMemberInfoResponseResult.setResult(dubboResult.getResult());
            queryMemberInfoResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryMemberInfoResponse queryMemberInfoResponse = new QueryMemberInfoResponse();
            queryMemberInfoResponseResult.setResult(queryMemberInfoResponse);
            queryMemberInfoResponse.setHeader(queryMemberInfoRequest.getHeader());
            queryMemberInfoResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryMemberInfoResponse.setRespCode(((BizException) e).getCode());
                queryMemberInfoResponse.setRespDesc(e.getMessage());
            } else {
                queryMemberInfoResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryMemberInfoResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryMemberInfoResponseResult;
    }

    /**
     * 会员明细信息查询Dubbo实现
     *
     * @param queryMemberDetailInfoRequest 会员明细信息查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryMemberDetailInfoResponse> 会员明细信息查询接口应答对象
     * @author Jinan Liu
     * @date 2020/6/19 15:17
     */
    @Override
    public Result<QueryMemberDetailInfoResponse> queryMemberDetailInfo(QueryMemberDetailInfoRequest queryMemberDetailInfoRequest) {

        DubboService<QueryMemberDetailInfoRequest> dubboService = new DubboServiceImpl();
        Result<QueryMemberDetailInfoResponse> queryMemberDetailInfoResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_MEMBER_DETAIL_INFO.getTxnNum().equals(queryMemberDetailInfoRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryMemberDetailInfoResponse> dubboResult = dubboService.callAtomService(queryMemberDetailInfoRequest,
                    MompTxnNumEnum.QUERY_MEMBER_DETAIL_INFO.getService(), QueryMemberDetailInfoResponse.class);
            queryMemberDetailInfoResponseResult.setResult(dubboResult.getResult());
            queryMemberDetailInfoResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryMemberDetailInfoResponse queryMemberDetailInfoResponse = new QueryMemberDetailInfoResponse();
            queryMemberDetailInfoResponseResult.setResult(queryMemberDetailInfoResponse);
            queryMemberDetailInfoResponse.setHeader(queryMemberDetailInfoRequest.getHeader());
            queryMemberDetailInfoResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryMemberDetailInfoResponse.setRespCode(((BizException) e).getCode());
                queryMemberDetailInfoResponse.setRespDesc(e.getMessage());
            } else {
                queryMemberDetailInfoResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryMemberDetailInfoResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryMemberDetailInfoResponseResult;
    }

    /**
     * 会员卡券信息查询Dubbo实现
     *
     * @param queryMemberCouponInfoRequest 会员卡券信息查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryMemberCouponInfoResponse> 会员卡券信息查询接口应答对象
     * @author Jinan Liu
     * @date 2020/6/19 15:16
     */
    @Override
    public Result<QueryMemberCouponInfoResponse> queryMemberCouponInfo(QueryMemberCouponInfoRequest queryMemberCouponInfoRequest) {

        DubboService<QueryMemberCouponInfoRequest> dubboService = new DubboServiceImpl();
        Result<QueryMemberCouponInfoResponse> queryMemberCouponInfoResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_MEMBER_COUPON.getTxnNum().equals(queryMemberCouponInfoRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryMemberCouponInfoResponse> dubboResult = dubboService.callAtomService(queryMemberCouponInfoRequest,
                    MompTxnNumEnum.QUERY_MEMBER_COUPON.getService(), QueryMemberCouponInfoResponse.class);
            queryMemberCouponInfoResponseResult.setResult(dubboResult.getResult());
            queryMemberCouponInfoResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryMemberCouponInfoResponse queryMemberCouponInfoResponse = new QueryMemberCouponInfoResponse();
            queryMemberCouponInfoResponseResult.setResult(queryMemberCouponInfoResponse);
            queryMemberCouponInfoResponse.setHeader(queryMemberCouponInfoRequest.getHeader());
            queryMemberCouponInfoResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryMemberCouponInfoResponse.setRespCode(((BizException) e).getCode());
                queryMemberCouponInfoResponse.setRespDesc(e.getMessage());
            } else {
                queryMemberCouponInfoResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryMemberCouponInfoResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryMemberCouponInfoResponseResult;
    }
}
